package com.dhms.vostok1.utils

import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View


object ClickSpanUtil {
    fun setClickSpan(
        spannableString: SpannableString,
        startIndex: Int,
        endIndex: Int,
        color: Int,
        clickListener: () -> Unit
    ) {
        spannableString.setSpan(
            ClickSpan(color, clickListener),
            startIndex,
            endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }

    class ClickSpan(var colors: Int, var clickListener: () -> Unit) : ClickableSpan() {
        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            //设置文本的颜色
            ds.color = colors
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.isUnderlineText = false
        }

        override fun onClick(widget: View) {
            clickListener.invoke()
        }
    }
}